#include "..\StdAfx.h"
#include "ThinkingView.h"

DXMesh ThinkingView::s_mesh;

HRESULT ThinkingView::CreateMesh(LPDIRECT3DDEVICE9 pd3ddev9)
{
	HRESULT hr = D3DXCreateBox(pd3ddev9, 15.0f, 15.0f, 0.01f, &s_mesh.m_pMesh, &s_mesh.m_pAdjacency);

	if (hr == S_OK)
	{
		s_mesh.m_pMesh->OptimizeInplace(
			D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
			( DWORD* )s_mesh.m_pAdjacency->GetBufferPointer(), NULL, NULL, NULL );
	}

	LPD3DXMESH pNewMesh = NULL;

	// Optimize the mesh for performance
	if( FAILED( hr = s_mesh.m_pMesh->OptimizeInplace(
		D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
		( DWORD* )s_mesh.m_pAdjacency->GetBufferPointer(), NULL, NULL, NULL ) ) )
	{
		s_mesh.m_pAdjacency->Release();
		return hr;
	}

	// Remake mesh
	if ( SUCCEEDED( s_mesh.m_pMesh->CloneMeshFVF(s_mesh.m_pMesh->GetOptions(),
		(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2),
		pd3ddev9, &pNewMesh) ) )
	{
		s_mesh.m_pMesh->Release();
		s_mesh.m_pMesh = pNewMesh;

		LPVERTEXINFO_TEX2 pVertexInfo = NULL;

		// lock vertex buffer
		if ( SUCCEEDED( s_mesh.m_pMesh->LockVertexBuffer(D3DLOCK_DISCARD, (LPVOID*)&pVertexInfo) ) )
		{
			// set uv
			pVertexInfo[20].tu = pVertexInfo[20].tu2 = 0.0f;
			pVertexInfo[20].tv = pVertexInfo[20].tv2 = 1.0f;

			pVertexInfo[21].tu = pVertexInfo[21].tu2 = 0.0f;
			pVertexInfo[21].tv = pVertexInfo[21].tv2 = 0.0f;

			pVertexInfo[22].tu = pVertexInfo[22].tu2 = 1.0f;
			pVertexInfo[22].tv = pVertexInfo[22].tv2 = 0.0f;

			pVertexInfo[23].tu = pVertexInfo[23].tu2 = 1.0f;
			pVertexInfo[23].tv = pVertexInfo[23].tv2 = 1.0f;

			// unlock
			s_mesh.m_pMesh->UnlockVertexBuffer();
		}

		// create texture
		hr = D3DXCreateTextureFromFileEx(pd3ddev9, _T(".\\Resource\\thought-cloud.png"),
			D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER, 
			D3DX_DEFAULT, 0, NULL, NULL, &s_mesh.m_pTextures);
	}

	// set materials
	s_mesh.m_pMaterials = new D3DXMATERIAL();
	ZeroMemory(s_mesh.m_pMaterials, sizeof(D3DXMATERIAL));
	
	s_mesh.m_pMaterials->MatD3D.Emissive.a = 1.0f;
	s_mesh.m_pMaterials->MatD3D.Emissive.r = 1.0f;
	s_mesh.m_pMaterials->MatD3D.Emissive.g = 1.0f;
	s_mesh.m_pMaterials->MatD3D.Emissive.b = 1.0f;

	//s_mesh.m_pMaterials->MatD3D.Specular.a = 1.0f;
	//s_mesh.m_pMaterials->MatD3D.Specular.r = 0.75f;
	//s_mesh.m_pMaterials->MatD3D.Specular.g = 0.75f;
	//s_mesh.m_pMaterials->MatD3D.Specular.b = 0.75f;

	//s_mesh.m_pMaterials->MatD3D.Diffuse.a = 1.0f;
	//s_mesh.m_pMaterials->MatD3D.Diffuse.r = 0.5f;
	//s_mesh.m_pMaterials->MatD3D.Diffuse.g = 0.5f;
	//s_mesh.m_pMaterials->MatD3D.Diffuse.b = 0.5f;

	return hr;
}



ThinkingView::ThinkingView(void)
	: m_pTexture2(NULL)
{
}

ThinkingView::~ThinkingView(void)
{
}

HRESULT ThinkingView::Draw(LPDIRECT3DDEVICE9 pd3ddev9)
{
	pd3ddev9->SetTransform(D3DTS_WORLD, &m_world);
	pd3ddev9->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);		// always render(top most) next objects.
	s_mesh.Draw(pd3ddev9);
	pd3ddev9->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);	// Set to defalut.
	return S_OK;
}

void ThinkingView::SetPos(float x, float y, float z)
{
	D3DXVECTOR3 vecCam(0.0f, 175.0f, -175.0f);	// TODO: Get the Camera position.
	D3DXVECTOR3 vecPos(x, y, z);
	D3DXVECTOR3 vecDst = vecPos - vecCam;
	D3DXMATRIX matRotX, matRotY, matMoveY, matMoveXZ;

	D3DXMatrixRotationX(&matRotX, atan2f(-vecDst.y, vecDst.z));
	D3DXMatrixRotationY(&matRotY, atan2f(vecDst.x, vecDst.z));
	D3DXMatrixTranslation(&matMoveY, 0.0f, 20.0f, 0.0f);
	D3DXMatrixTranslation(&matMoveXZ, x, 0.0f, z);

	D3DXMATRIX matTrans = matMoveY * matRotY * matRotX;

	matTrans._14 = 0.0f;
	matTrans._34 = 0.0f;

	m_world = matTrans * matMoveXZ;
}